import tip from './tip';
// import qqmapsdk from 'qqmap-wx-jssdk';


//rgb转hex
let base_util = {};


base_util.loading = function () {
    wx.showLoading({
        title: '加载中'
    })
}

base_util.loaded = function () {
    wx.hideLoading({})
}

base_util.componentToHex = function (c) {
    let hex = c.toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}

base_util.rgbToHex = function (rgb) {

    let rgbArr = rgb.split(',');
    return "#" + base_util.componentToHex(rgbArr[0]) + base_util.componentToHex(rgbArr[1]) + base_util.componentToHex(rgbArr[2]);
}

//是否登录
base_util.isLogin = function () {
    let resule;
    let userInfo = base_util.getStorageSync('userInfo');

    if (userInfo) {

        let json = JSON.parse(userInfo);

        if (json.user_info.userId > 0) {
            resule = json.user_info.userId;

        } else {
            resule = json.user_info.userId;
        }
    }

    return resule;
}
//日期转换
base_util.formatDateTime = function (inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + '　' + h + ':' + minute + ':' + second;
}

//获取当前日期
base_util.getNowFormatDate = function () {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;


}

/*获取当前页url*/
base_util.getCurrentPageUrl = function () {
    let pages = getCurrentPages()    //获取加载的页面
    let currentPage = pages[pages.length - 1]    //获取当前页面的对象
    let url = currentPage.route;   //当前页面url
    return url
}


/*获取当前页带参数的url*/
base_util.getCurrentPageUrlWithArgs = function () {
    let pages = getCurrentPages()    //获取加载的页面
    let currentPage = pages[pages.length - 1]    //获取当前页面的对象
    let url = currentPage.route    //当前页面url
    let options = currentPage.options    //如果要获取url中所带的参数可以查看options

    //拼接url的参数
    let urlWithArgs = url + '?'
    for (let key in options) {
        let value = options[key]
        urlWithArgs += key + '=' + value + '&'
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

    return urlWithArgs
}
/*获取url上的参数*/
base_util.getURLParam = function (link, name) {
    let args = {};
    let query = link.split('?')[1];
    if (!query) {
        return '';
    }
    if (query.lastIndexOf('#') > -1) {
        query = query.split('#')[0];
    }
    let pairs = query.split("&");
    for (let i = 0; i < pairs.length; i++) {
        let pos = pairs[i].indexOf('=');
        if (parseInt(pos) === -1) continue;
        let argname = pairs[i].substring(0, pos);
        let value = pairs[i].substring(pos + 1);
        args[argname] = decodeURIComponent(value);
    }
    return args[name];
};


base_util.getCurrentUrlParam = function (name) {
    let link = base_util.getCurrentPageUrlWithArgs();

    return base_util.getURLParam(link, name);
};


base_util.dateDiff = function (sDate1, sDate2) {    //sDate1和sDate2是2002-12-18格式
    let aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])    //转换为12-18-2002格式
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数
    return iDays
}
/*数据缓存*/
base_util.setStorageSync = function (key, value) {
    try {
        wx.setStorageSync(key, value)
    } catch (e) {
        console.log(e);
    }
};

base_util.getStorageSync = function (key) {
    let value;
    try {
        value = wx.getStorageSync(key)
        if (value) {
            // Do something with return value
        }
    } catch (e) {
        console.log(e);
    }
    return value;
};

base_util.removeStorageSync = function (key) {
    try {
        wx.removeStorageSync(key)
    } catch (e) {
        console.log(e);
    }
};

// 页面跳转
base_util.pageJump = function (url, tab) {
    if (url) {
        if (tab) {
            wx.switchTab({
                url: url
            })
        } else {
            wx.navigateTo({
                url: url
            })
        }

    } else {
        tip.alert('敬请期待~')
    }
};


export default base_util;